import request from '../utils/request'

export function listDriver(query) {
  return request({
    url: '/drivermgr/list',
    method: 'get',
    params: query
  })
}

export function createDriver(data) {
  return request({
    url: '/drivermgr/create',
    method: 'post',
    data
  })
}

export function updateDriver(data) {
  return request({
    url: '/drivermgr/update',
    method: 'post',
    data
  })
}

export function deleteDriver(data) {
  return request({
    url: '/drivermgr/delete',
    method: 'delete',
    params: data
  })
}

export function queryAllCompany() {
  return request({
    url: '/drivermgr/queryCompany',
    method: 'get'
  })
}

export function queryByDriverisLicense(driverLicense) {
  return request({
    url: '/drivermgr/queryByIdCard',
    method: 'get',
    params: { idCard: driverLicense }
  })
}
// 由于返回的数据和期望的数据不一致，因此在catch里面下载文件
export function downloadExcel(query) {
  return request({
    url: '/drivermgr/downloadExcel',
    method: 'post',
    params: query,
    responseType: 'blob'
  }).catch(res => {
    console.log(res)
    var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
    if (window.navigator.msSaveBlob) {
      window.navigator.msSaveBlob(blob, '司机列表.xlsx')
    } else {
      var downloadElement = document.createElement('a')
      var href = window.URL.createObjectURL(blob) // 创建下载的链接

      downloadElement.href = href
      downloadElement.download = '司机列表.xlsx'

      document.body.appendChild(downloadElement)
      downloadElement.click()

      document.body.removeChild(downloadElement) // 下载完成移除元素

      window.URL.revokeObjectURL(href) // 释放掉blob对象
    }
  })
}

